package rexsee.fileSelector;

import java.io.File;
import java.util.HashMap;

import rexsee.core.browser.Browser;
import rexsee.core.style.RexseeDrawable;
import rexsee.core.style.StyleSheet;
import rexsee.fileSelector.MediaStoreCache.Thumb;
import rexsee.natives.ViewStyle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileSelectorItem extends LinearLayout {

	private static final int ICON_BOUNDS = 90;

	public final Browser browser;
	public final Context context;
	public final ImageView icon;
	public final TextView name;
	public final ImageView select;

	public FileSelectorItem(Browser browser, StyleSheet iconStyle) {

		super(browser.getContext());

		this.browser = browser;
		context = browser.getContext();

		setOrientation(LinearLayout.HORIZONTAL);
		setGravity(Gravity.CENTER_VERTICAL);

		icon = new ImageView(context);
		ViewStyle.setImageViewStyle(browser, icon, iconStyle);
		icon.setLayoutParams(iconStyle.getIconLayoutParams());
		icon.setScaleType(ScaleType.FIT_CENTER);
		addView(icon);

		name = new TextView(context);
		addView(name);

		select = new ImageView(context);
		addView(select);

	}

	public void set(Drawable iconDrawable, String text, StyleSheet styleSheet, boolean showSelect) {
		if (icon != null) icon.setImageDrawable(iconDrawable);
		ViewStyle.setTextViewStyle(browser, name, styleSheet);
		name.setLayoutParams(styleSheet.getLabelLayoutParams());
		name.setText(text);
		if (showSelect) {
			ViewStyle.setImageViewStyle(browser, select, styleSheet);
			select.setLayoutParams(styleSheet.getIconLayoutParams());
			select.setVisibility(View.VISIBLE);
		} else {
			select.setVisibility(View.GONE);
		}
	}

	public static final String[] imageExtensions = new String[]{"jpg", "jpeg", "png", "gif"};
	public static boolean isImage(String filename) {
		return isWithExtension(imageExtensions, filename);
	}
	public static boolean isWithExtension(String[] exts, String filename) {
		if (exts == null) return true;
		filename = filename.toLowerCase();
		for (int i = 0; i < exts.length; i++) {
			String ext = exts[i].trim().toLowerCase();
			if (!ext.equals("") && filename.endsWith("." + ext)) return true;
		}
		return false;
	}
	public static Drawable getFileIcon(Browser browser, HashMap<String, String> mIcons, Drawable defaultIcon, String path) {
		String ext = path.substring(path.lastIndexOf(".") + 1);
		if (isImage(path)) {
			if (mIcons != null && mIcons.containsKey(ext)) {
				return RexseeDrawable.getDrawable(browser, mIcons.get(ext));
			} else {
				File file = new File(Uri.parse(path).getSchemeSpecificPart());
				if (file != null && file.isFile() && file.canRead()) {
					try {
						Thumb thumbnail = browser.mediaStore.getImageThumbnailFromMediaStore(browser.getContext(), file.getAbsolutePath());
						if (thumbnail != null) {
							Bitmap bitmap = RexseeDrawable.createBitmapBySpecifiedOrientation(thumbnail.path, ICON_BOUNDS, thumbnail.orientation);
							return new BitmapDrawable(bitmap);
						} else {
							return new BitmapDrawable(RexseeDrawable.createBitmapByOrientation(path, ICON_BOUNDS));
						}
					} catch (Exception e) {
						return defaultIcon;
					}

				} else {
					return defaultIcon;
				}
			}
		} else {
			if (mIcons != null && mIcons.containsKey(ext)) {
				return RexseeDrawable.getDrawable(browser, mIcons.get(ext));
			} else {
				return defaultIcon;
			}
		}
	}

}
